﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;

namespace Hr.Controllers
{
    //interview_status:-1:注销 0:默认 1:登记 2:申请录用 3:审批 4:成功
    public class interviewController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly MyDbContext myDbContext;
        private readonly Iengage_resumeServices erServices;
        private readonly Iengage_interviewServices eiServices;

        public interviewController(MyDbContext myDbContext, ILogger<clientController> logger,Iengage_resumeServices erServices, Iengage_interviewServices eiServices)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.erServices = erServices;
            this.eiServices = eiServices;
        }

        [HttpGet]
        public IActionResult interview_list()
        {
            string where = "1=1 and check_status = '1'";
            ViewData["where"] = where;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> interview_list(int currentPage, string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }


        [HttpGet]
        public IActionResult interview_register(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> interview_register1(int id)
        {
            Dictionary<string, object> di = new Dictionary<string, object>();
            engage_resume sks = await erServices.SelectID(id);
            //var fd = await myDbContext.engage_resume.FindAsync(id);
            List<engage_interview> awm = await eiServices.SelectAll();
            engage_interview view = awm.Where(e => e.resume_id == id).FirstOrDefault();
            int interview_amount = 0;
            if (view == null)
            {
                interview_amount = 1;
            }
            else
            {
                interview_amount = view.interview_amount + 1;
            }
            di["num"] = interview_amount;
            di["sks"] = sks;
            return Ok(di);
            //return Content(JsonConvert.SerializeObject(fd));
        }

        [HttpPut]
        public async Task<IActionResult> interview_register([FromForm] Eengage_interview test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    //test.interview_status = 1;
                    //myDbContext.engage_interview.Add(test);
                    //result = await myDbContext.SaveChangesAsync();
                        if (test.interview_amount > 1)
                        {
                            engage_interview e = await eiServices.SelWhere(test.resume_id);
                            e.interview_amount = test.interview_amount;
                            e.interview_status = 1;
                            e.check_status = 0;
                            e.checker = test.checker;
                            result = await eiServices.Update(e);
                    }else{
                        test.interview_status = 1;
                        myDbContext.engage_interview.Add(test);
                        result = await myDbContext.SaveChangesAsync();
                    }

                    List<engage_resume> er_list = await erServices.getOne_resume_Async(test.resume_id);
                    int s = await erServices.update_resume_status_Async(test.resume_id,test.interview_status);
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }

        [HttpGet]
        public async Task<ActionResult> UpdateZT(int id)
        {
            var fd = await myDbContext.engage_resume.FindAsync(id);
            myDbContext.engage_resume.Attach(fd);
            fd.interview_status = 1;
            myDbContext.Entry(fd).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            int result = await myDbContext.SaveChangesAsync();
            return Content(result.ToString());
        }

        //[HttpPost]
        //public async Task<IActionResult> sift_list([FromQuery] int id)
        //{
        //    Dictionary<string, object> di = new Dictionary<string, object>();
        //    FenYe fy = new FenYe()
        //    {
        //        Where = "",
        //        CurrentPage = id,
        //        PageSize = 3
        //    };
        //    List<engage_interview> list = await eiServices.FenYe(fy);
        //    di["list"] = list;
        //    di["rows"] = fy.Rows;
        //    di["pages"] = fy.Pages;
        //    return Ok(di);
        //}

        [HttpPost]
        public async Task<IActionResult> sift_list(int currentPage, string where)
        {
            Dictionary<string, object> list = await eiServices.FenYe_interview_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }

        [HttpGet]
        public IActionResult sift_list()
        {
            string where = "1=1 and interview_status = '1'";
            ViewData["where"] = where;
            return View();
        }

        [HttpGet]
        public IActionResult interview_sift(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> interview_sift1(int id)
        {
            List<engage_interview> ei_list = await eiServices.getOne_interview_Async(id);
            int eid = ei_list[0].resume_id;
            List<engage_resume> er_list = await erServices.getOne_resume_Async(eid);

            //var fd = await myDbContext.engage_interview.FindAsync(id);
            //var er = await myDbContext.engage_resume.FindAsync(id);
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["engage_resume"] = er_list;
            di["engage_interview"] = ei_list;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPut]
        public async Task<IActionResult> interview_sift([FromForm]engage_interview test)
        {
                int result;
                result = await eiServices.update_ms_Async(test);
                if (result > 0)
                {
                    List<engage_interview> ei_list = await eiServices.getOne_interview_Async(test.ein_id);
                    int rid = ei_list[0].resume_id;
                    int s = await erServices.update_resume(rid,test.interview_status,test.check_comment);
            }
            return Content(JsonConvert.SerializeObject(result));
        }
    }
}
